//
//  ContactDelete.m
//  HandyDialer
//
//	Developed by Team Synergy
//  Created by Jacky Li on 11/24/11.
//  Coders: Jacky Li and Jordan Tseng 
//  Graphic Designer: Derek Fong
//  Developer: Brad Lisson, Derek Fong, Jacky Li, Jeremy Gangoso, Jordan Tseng
//  Copyright HandyDialer 2011. All rights reserved.
//
#import "ContactDeleteViewController.h"

@implementation ContactDeleteViewController
@synthesize highlightArea;
@synthesize backgroundArea;
@synthesize contactInfo;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)dealloc {
	[highlightArea release];
    [super dealloc];
}

- (void)viewDidLoad 
{
	DataStorage *data = [DataStorage sharedData];
	currentContact = [data getCurrentContact];
	[self loadContactInfo];
    counter = 0;
	defaults = [NSUserDefaults standardUserDefaults];
	speed = [defaults floatForKey:@"Speed"];
	highlightColor = [defaults stringForKey:@"Highlight"];
	themeColor = [defaults stringForKey:@"Theme"];
	[backgroundArea setImage:[UIImage imageNamed: [themeColor stringByAppendingString:@"_confirm_delete.png"]]];
	[super viewDidLoad];
    
}

- (void)viewDidAppear:(BOOL)animated
{
	defaults = [NSUserDefaults standardUserDefaults];
    speed = [defaults floatForKey:@"Speed"];
	highlightColor = [defaults stringForKey:@"Highlight"];
	themeColor = [defaults stringForKey:@"Theme"];
    [backgroundArea setImage:[UIImage imageNamed: [themeColor stringByAppendingString:@"_confirm_delete.png"]]];
	
	if (uniTimer == nil)
		uniTimer = [NSTimer scheduledTimerWithTimeInterval:(speed) target:self selector:@selector(optionChange) userInfo:nil repeats:YES];
	[super viewDidAppear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[uniTimer invalidate];
	uniTimer = nil;
	[super viewDidDisappear:animated];
}

- (void)loadContactInfo
{
	contactInfo.text = [self getContactName];
	ABMultiValueRef contactNumbers = [self getContactProperty:kABPersonPhoneProperty];
	if (ABMultiValueGetCount(contactNumbers) > 0)
	{
		contactMainNumber = ABMultiValueCopyValueAtIndex(contactNumbers, 0);
		contactMainNumber = [self stripNumber:contactMainNumber];
	}
	for (int i = 0; i < ABMultiValueGetCount(contactNumbers); ++i)
	{
		NSLog(@"%@", ABMultiValueCopyLabelAtIndex(contactNumbers, i));
	}
	if (contactMainNumber != nil)
	{
		contactInfo.text = [contactInfo.text stringByAppendingFormat:@"\n%@",contactMainNumber];
	}
	else
	{
		contactInfo.text = [contactInfo.text stringByAppendingFormat:@"\n%@",@"(No Numbers)"];
	}
}

- (NSString *)getContactName
{
	return (NSString *)ABRecordCopyCompositeName(currentContact);
}

- (ABMultiValueRef)getContactProperty:(ABPropertyType)property
{
	return ABRecordCopyValue(currentContact,property);
}

// strips () and space from number for consistency and dialing purposes
- (NSString *)stripNumber:(NSString *)rawNumber
{
	if ([rawNumber length] > 1)
	{
		if ([rawNumber characterAtIndex:0] == '1' && [rawNumber characterAtIndex:1] == ' ')
		{
			rawNumber = [rawNumber substringFromIndex:3];
			rawNumber = [NSString stringWithFormat:@"1-%@-%@",[rawNumber substringToIndex:3],[rawNumber substringFromIndex:5]];
			NSLog(@"%@",rawNumber);
			
		}
		else if ([rawNumber characterAtIndex:0] == '(')
		{
			rawNumber = [rawNumber substringFromIndex:1];
			rawNumber = [NSString stringWithFormat:@"%@-%@",[rawNumber substringToIndex:3],[rawNumber substringFromIndex:5]];
		}
		return rawNumber;
	}
	else
	{
		return rawNumber;
	}
}

- (void)returnToView:(NSString *)viewName:(UIViewController *)previousView
{
	// recursively traverse through parent ViewControllers to find desired view
	previousView = [previousView presentingViewController];
	NSLog(@"%@", previousView.nibName);
	if (previousView.nibName == viewName)
	{
		[previousView dismissModalViewControllerAnimated:NO];
	}
	else
	{
		[self returnToView:(viewName):(previousView)];
	}
}

- (IBAction)switchView:(id)sender{
	switch (counter)
	{
		// delete contact
		case 1:
		{
			counter = 0;
			// create temporary AddressBook
			ABAddressBookRef tempBook = ABAddressBookCreate();
			// remove contact record from AddressBook
			ABAddressBookRemoveRecord(tempBook, currentContact, nil);
			// save updated AddressBook to iOS
			ABAddressBookSave(tempBook, nil);
			// return to contact menu
			[self returnToView:@"ContactView":self];
			break;
		}
		// cancel
		case 2:
		{
			counter = 0;
			[self dismissModalViewControllerAnimated:NO];
			break;
		}
	}
}

- (void)highlightChange:(NSString *)picture
{
    UIImage *image = [UIImage imageNamed: picture];
    [highlightArea setImage:image];
	
}

// Decides which screen to go to
-(void)optionChange 
{
    if (counter == 2)
    {
        counter = 0;
    }
    counter += 1;
    NSString *next = highlightColor;
	next = [next stringByAppendingString:@"_confirm_delete_"];  
	next = [next stringByAppendingString:[NSString stringWithFormat:@"%d", counter]];
	next = [next stringByAppendingString:@".png"];
	[self highlightChange:next];
}


@end
